package com.aisino.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;

import com.aisino.util.BigDecimalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.aisino.entity.TOrderData;
import com.aisino.entity.TSysorderheadWithBLOBs;
import com.aisino.entity.TSysorderline;
import com.aisino.mapper.HeBingMapper;
import com.aisino.mapper.SelectMapper;
import com.aisino.mapper.TOrderDataPcDjMapper;
import com.aisino.mapper.TSysorderheadMapper;
import com.aisino.mapper.TSysorderlineMapper;
import com.aisino.util.HandleDataUtil;

@Service
public class AutoService {
	@Resource
    private HeBingMapper heBingMapper;
    @Resource
    private SelectMapper selectMapper;
    @Resource
    private HeBingService heBingService;

    @Resource
    private TSysorderlineMapper tSysorderlineMapper;
    
    @Resource
    private TSysorderheadMapper tSysorderheadMapper;
    
    @Resource
    private TOrderDataPcDjMapper tOrderDataPcDjMapper;

    static final String maxMoney = "100000";

    static final BigDecimal max = new BigDecimal(maxMoney);

    static final BigDecimal maxChaiFenJe = new BigDecimal("99999");
    
	public void autoChaiFenHeBing(String DJHM) throws Exception{
        // 事先需要获取到发票头
        Map<String, String> map = new HashMap<>();
        map.put("DJHM", DJHM);
        map.put("OHID", "");
        map.put("isNega", "");// 是否查询负数
        List<TOrderData> list = heBingService.heBingReturnData(DJHM);
        

        System.out.println("获取数据" + list);

        // 新的明细
        List<List<TOrderData>> newList = new ArrayList<>();


        // 记录待剔除明细行
        Set<Integer> quchuSet = new HashSet<>();

        BigDecimal jshj = new BigDecimal("0");

        // 计算加税合计等
        for (int i = 0; i < list.size(); i++) {
            TOrderData tOrderData = list.get(i);
            BigDecimal temp = BigDecimalUtil.add(jshj, tOrderData.getJE());
            jshj = temp;
        }
        // 记录list
        // list循环值没有固定导致无限蹦张
        int size = list.size();

        // 如果加税合计超限就需要拆分
        ChaiFenService.chaiFen(jshj, size, list, quchuSet, newList);

        // 输出最后结果
        HandleDataUtil.soutData(newList);
        // 储存数据
        cunchuData(newList);
    }
	
	public void cunchuData(List<List<TOrderData>> newList) {
    	System.out.println("储存数据"+newList.size());
        for(int q=0;q<newList.size();q++) {
            TOrderData tOrderData = new TOrderData();

            String OHID="123"+UUID.randomUUID().toString().replace("-", "");
            BigDecimal JSHJ = new BigDecimal("0");
            BigDecimal HJJE = new BigDecimal("0");
            BigDecimal HJSE = new BigDecimal("0");

            for (int w = 0; w < newList.get(q).size(); w++) {

                // 获取到一个原始数据
                tOrderData = newList.get(q).get(w);
                System.out.println("原始数据"+tOrderData);

                TSysorderline tSysorderline = new TSysorderline();
                // 复制两个对象中相同属性的值
                BeanUtils.copyProperties(tOrderData, tSysorderline);

                // 插入数据
                tSysorderline.setOLID("123" + UUID.randomUUID().toString().replace("-", ""));
                tSysorderline.setOHID(OHID);
                // 计算不含税单价
                tSysorderline.setBHSDJ(tSysorderline.getDJ().multiply(new BigDecimal("100").subtract(tSysorderline.getSLV()).divide(new BigDecimal("100"))));
                // 不含税金额
                BigDecimal BHSJE=tSysorderline.getJE().multiply(new BigDecimal("100").subtract(tSysorderline.getSLV()).divide(new BigDecimal("100")));
                tSysorderline.setBHSJE(BHSJE);
                // 计算税额
                tSysorderline.setSE(tSysorderline.getJE().subtract(BHSJE));
                tSysorderline.setSPXH(w+1);

                // 保存商品头需要信息
                JSHJ = BigDecimalUtil.add(JSHJ, tSysorderline.getJE());
                HJJE = BigDecimalUtil.add(HJJE, tSysorderline.getBHSJE());
                HJSE = BigDecimalUtil.add(HJSE, tSysorderline.getSE());
//	                tSysorderline.setCREATETIME(new Date());
                tSysorderlineMapper.insertSelective(tSysorderline);
            }

            // 添加完商品行开始添加商品头
            TSysorderheadWithBLOBs tSysorderhead = new TSysorderheadWithBLOBs();
            // 复制两个对象中相同属性的值
            BeanUtils.copyProperties(tOrderData, tSysorderhead);
            tSysorderhead.setOHID(OHID);
            tSysorderhead.setJSHJ(JSHJ);
            tSysorderhead.setHJJE(HJJE);
            tSysorderhead.setHJSE(HJSE);
            System.out.println("储存一张票"+tSysorderhead.getDJHM());
            tSysorderheadMapper.insertSelective(tSysorderhead);
        }
    }
}
